package com.ruoyi.web.controller.message;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.framework.config.RabbitmqConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/amqp")
public class SendAmqpMessageController
{
    @Autowired
    private RabbitTemplate rabbitmqTemplate;

    @GetMapping("/sendDirectMessage")
    public AjaxResult sendDirectMessage(String message)
    {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "msg-control:"+message;
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map = new HashMap<>();
        map.put("meesageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        map.put("exchange", RabbitmqConfig.EXCHANGE_TOPICS_INFORM);
        map.put("routingkey", RabbitmqConfig.ROUTINGKEY_SMS);
        // 将消息携带绑定键值： TestDirectRouting 发送交换机TestDirectExchange
        rabbitmqTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_TOPICS_INFORM, RabbitmqConfig.ROUTINGKEY_SMS, map);
        return AjaxResult.success("OK");
    }
}
